<?php

/**
 *author：william
 *新闻资讯
 */

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\News as NewsModel;
use Illuminate\Http\Request;

class NewsController extends Controller
{
    public function list(Request $request)
    {
        $page = $request->get('page'); //要查询的页数
        if(empty($page) && !is_numeric($page)){
            return $this->fail(['message'=>'查询参数错误']);
        }
        //偏移量
        $pian = 7;
        //分页查询数据
        $data = NewsModel::orderBy('created_at','desc')->paginate($pian);
        $data && $data  = $data->toArray();
        if ($page > $data['last_page']) {
            return $this->success('成功',$data);
        }
        //因为分页查询到的数据里面有一个参数是  last_page  是计算了数据库中的总页数
        return $this->success('成功',[$data['data'], 'last_page' => $data['last_page']]);
    }
}
